// src/utils/message.js
import Vue from 'vue';
import MessageComponent from '@/components/common/Meassage/index.vue';

let instance = null;

function getInstance() {
  if (!instance) {
    const Constructor = Vue.extend(MessageComponent);
    instance = new Constructor().$mount(document.createElement('div'));
    document.body.appendChild(instance.$el);
  }
  return instance;
}

// 创建一个函数，作为默认调用
function showMessage(message) {
  const instance = getInstance();
  if (typeof message === 'string') {
    instance.show({ message, type: 'info' });
  } else {
    instance.show(message);
  }
  return showMessage; // 支持链式调用（虽然一般不用）
}

// 添加方法到函数对象上
showMessage.success = function (message) {
  const instance = getInstance();
  instance.show({ message, type: 'success' });
  return showMessage;
};

showMessage.error = function (message) {
  const instance = getInstance();
  instance.show({ message, type: 'error' });
  return showMessage;
};

showMessage.warning = function (message) {
  const instance = getInstance();
  instance.show({ message, type: 'warning' });
  return showMessage;
};

showMessage.info = function (message) {
  const instance = getInstance();
  instance.show({ message, type: 'info' });
  return showMessage;
};

export default showMessage;
